package com.cskaoyan.mall.controller;

import com.cskaoyan.mall.model.Admin;
import com.cskaoyan.mall.model.Result;
import com.cskaoyan.mall.service.AdminService;
import com.cskaoyan.mall.service.AdminServiceImpl;
import com.cskaoyan.mall.utils.HttpUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * @PackgeName: ${PACKAGE_NAME}
 * @ClassName: ${NAME}
 * @Author: admin
 * Date: 2020/4/29 14:39
 * project name: Javaproject1
 * @Version:
 * @Description:
 */
@WebServlet("/api/admin/admin/*")
public class AdminServlet extends HttpServlet {

    private AdminService adminService = new AdminServiceImpl();

    Gson gson = new Gson();


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //String servletPath = request.getServletPath();
        String requestURI = request.getRequestURI();
        System.out.println(requestURI);
        //getPathInfo()直接获得*的路径，代替getServletPath()可行？
        String action = requestURI.replace("/api/admin/admin/", "");
        if ("login".equals(action)) {
            login(request,response);
        } else if ("addAdminss".equals(action)) {
            addAdminss(request, response);
        } else if ("getSearchAdmins".equals(action)) {
            getSearchAdmins(request, response);
        } else if ("updateAdminss".equals(action)) {
            updateAdminss(request, response);
        }

    }

    /**
     * 修改管理员账号
     *
     * 1.抓包
     * 2.分析逻辑
     * 3.返回响应体
     * @param request
     * @param response
     *
     * 问题：若账号为空？若查找值仅有一个？
     */
    private void updateAdminss(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        Admin admin = gson.fromJson(requestBody, Admin.class);

        int code = adminService.updateAdminss(admin);

        Result result = new Result();
    }

    /**
     * 多条件查询管理员账号
     *
     * 1.抓包
     * 2.分析逻辑
     * 3.
     * @param request
     * @param response
     *
     * 问题：若账号为空？若查找值仅有一个？
     */
    private void getSearchAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        Admin admin = gson.fromJson(requestBody, Admin.class);

        //如果什么都没有输入，该不该给前端传值刷新页面？

        List<Admin> admins = adminService.getSearchAdmins(admin);

        //
        Result result = new Result();
        result.setCode(0);
        result.setData(admins);
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 创建账号的具体业务逻辑
     *
     * 1.抓包：抓请求报文（服务器上面
     * 2.分析逻辑
     * 3.返回响应体
     * @param request
     * @param response
     *
     * 问题：正则表达式
     */
    private void addAdminss(HttpServletRequest request, HttpServletResponse response) throws IOException {

        //IO流
        String requestBody = HttpUtils.getRequestBody(request);
        Admin admin = gson.fromJson(requestBody, Admin.class);

        //code service的返回,  service的code是dao的返回
        int code = adminService.addAdminss(admin);

        Result result = new Result();
        if (code == 200) {
            HashMap<Object, Object> map = new HashMap<>();
            map.put("email", admin.getEmail());
            map.put("nickname", admin.getNickname());
            map.put("pwd", admin.getPwd());

            result.setData(map);
            result.setMessage("创建管理员成功！");
        } else {
            result.setMessage("该账号不允许重复使用");
        }
    }

    /**
     * 登录的具体业务逻辑
     *
     * 1.抓包：抓请求报文（服务器上面，local host）
     * 2.分析逻辑
     * 3.返回响应报文（响应体，格式是什么样子，这个时候就要抓包服务器）
     * @param request
     * @param response
     */
    private void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //IO流

        String requestBody = HttpUtils.getRequestBody(request);

        //Admin类型接收该请求体
        Admin admin = gson.fromJson(requestBody, Admin.class);
        System.out.println(admin);

        //code service的返回,  service的code是dao的返回
        int code = adminService.login(admin);

        Result result = new Result();
        if (code == 200) {
            //返回正确的信息

            //?
            result.setCode(0);
            HashMap<Object, Object> map = new HashMap<>();
            map.put("token", admin.getEmail());
            map.put("name", admin.getEmail());
            result.setData(map);
        } else {
            //返回错误的信息 需要返回10000
            result.setCode(10000);
            result.setMessage("服务器异常");
        }

        response.getWriter().println(gson.toJson(result));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //String servletPath = request.getServletPath();
        String requestURI = request.getRequestURI();
        //getPathInfo()直接获得*的路径，代替getServletPath()可行？
//        String action = requestURI.replace("api/admin/admin", "");
        String action = requestURI.replace("/api/admin/admin/", "");

        if ("allAdmins".equals(action)) {
            allAdmin(request,response);
        } else if ("deleteAdmins".equals(action)) {
            deleteAdmins(request, response);
        } else if ("getAdminsInfo".equals(action)) {
            getAdminsInfo(request, response);
        }


    }

    /**
     * 删除管理员账号
     * 1.抓包，请求参数为该管理员id
     * 2.分析逻辑
     * 3.抓包查看响应体应该是什么
     * @param request
     * @param response
     *
     * 问题：如何取get参数？
     */
    private void getAdminsInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {

        Result result = new Result();
        result.setCode(0);
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 删除管理员账号
     * 1.抓包，请求参数为该管理员id
     * 2.分析逻辑
     * 3.抓包查看响应体应该是什么
     * @param request
     * @param response
     *
     * 问题：如何取get参数？
   */
    private void deleteAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {


        Result result = new Result();
        result.setCode(0);
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 展示所有管理员的信息
     * 1.抓包--->没有请求参数
     * 2.分析逻辑
     * 3.抓包，查看响应体应该是什么
     * @param request
     * @param response
     */
    private void allAdmin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Admin> adminList = adminService.allAdmins();
        Result result = new Result();
        result.setCode(0);
        result.setData(adminList);
        response.getWriter().println(gson.toJson(result));
    }
}
